package cn.aicnn.chatssespringboot.service.Impl;

import cn.aicnn.chatssespringboot.dto.Account;
import cn.aicnn.chatssespringboot.dto.User;
import cn.aicnn.chatssespringboot.service.AccountService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.aicnn.chatssespringboot.persistence.accountMapper;
import cn.aicnn.chatssespringboot.persistence.userMapper;
import java.util.Map;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private accountMapper accountMapper;

    @Autowired
    private userMapper userMapper;

    @Override
    public boolean register(Account account) {
        User existingUser = userMapper.selectByUsername(account.getUsername());
        if (existingUser != null) {
            return false; // 用户名已存在，注册失败
        }

        User user = new User();
        user.setUsername(account.getUsername());
        user.setPassword(account.getPassword());
        account.setPhone("1"); // 设置默认值为 1
        account.setFirstname("1"); // 设置默认值为 1
        account.setLastname("1"); // 设置默认值为 1
        account.setAdmin(1);


        int rowsAffected = accountMapper.insert(account);
        int rowsAffectedUser= userMapper.insert(user);
        return rowsAffected > 0; // 注册成功返回true，失败返回false
    }

    @Override
    public Account getUserInfo(String username) {
        return accountMapper.selectByUsername(username);
    }

    @Override
    public boolean updateUserInfo(String username, Account newInfo) {
        Account existingUser = accountMapper.selectByUsername(username);
        if (existingUser != null && newInfo != null && !newInfo.getUsername().isEmpty() && !newInfo.getPassword().isEmpty()
                && !newInfo.getPhone().isEmpty() && !newInfo.getAddress().isEmpty() && !newInfo.getFirstname().isEmpty()
                && !newInfo.getLastname().isEmpty() && !newInfo.getQuestion().isEmpty() && !newInfo.getAnswer().isEmpty()) {
            newInfo.setUsername(username);
            int rowsAffected = accountMapper.updateUserInfo(newInfo);
            return rowsAffected > 0; // 更新成功返回true，失败返回false
        }
        return false; // 更新失败
    }

    @Override
    public String getSecurityQuestion(String username) {
        return accountMapper.selectQuestionByUsername(username);
    }

    @Override
    public boolean verifyAnswer(String username, String answer) {
        String storedAnswer = accountMapper.selectAnswerByUsername(username);
        return storedAnswer != null && storedAnswer.equals(answer);
    }

    @Override
    public boolean updatePassword(String username, String password, String answer) {
        Account account = accountMapper.selectByUsernameAndAnswer(username, answer);
        if (account != null) {
            account.setPassword(password);
            accountMapper.updateById(account); // 更新Account表中的密码

            // 调用UserMapper更新用户密码
            userMapper.updatePassword(username, password);

            return true;
        }
        return false;
    }

    @Override
    public boolean isAdmin(String username) {
        Account user = accountMapper.selectOne(new QueryWrapper<Account>().eq("username", username));
        // 判断 admin 字段是否为 1，表示ip可用
        return user != null && user.getAdmin() == 1;
    }

    @Override
    public boolean checkUserExist(String username) {
        Account user = accountMapper.selectOne(new QueryWrapper<Account>().eq("username", username));
        return user != null;
    }

    @Override
    public boolean checkUserExists(String username) {
        Account user = accountMapper.selectByUsername(username);
        return user != null;
    }

    @Override
    public int updateAddressByUsername(String username, String address) {
        return accountMapper.updateAddressByUsername(username, address);
    }

    @Override
    public boolean setAdmin(String username, int adminValue) {
        Account account = accountMapper.selectOne(new QueryWrapper<Account>().eq("username", username));
        if (account != null) {
            account.setAdmin(adminValue);
            int rowsAffected = accountMapper.updateById(account);
            return rowsAffected > 0;
        }
        return false;
    }

}
